<?php


namespace app\dao\query;


use app\model\Hotel;

class HotelQueryDao
{
    private $query;

    public function __construct()
    {
        $this->query = new Hotel();
    }

    /**
     * 获取列表
     * @param int $page -
     * @param int $limit -
     * @return \think\Paginator -
     */
    public function getList(int $page = 1, int $limit = 10)
    {
        return $this->query->paginate([
            'list_rows' => $limit,
            'page' => $page
        ]);
    }

    /**
     * 获取所有数据
     * @return array -
     */
    public function getAll()
    {
        return $this->query->select();
    }

    /**
     * 获取一个
     * @return mixed
     */
    public function getOne()
    {
        return $this->query->find();
    }


    /**
     * 获取当前筛选下的总记录数
     * @return int -
     */
    public function getCount()
    {
        return $this->query->count();
    }

    /**
     * 根据id查询
     * @return $this
     */
    public function filterById($id){
        if ($id != null){
            $this->query = $this->query->where('id' , $id);
        }
        return $this;
    }

    public function fuzzyByName(String $name){
        if ($name != null){
            $this->query = $this->query->whereLike('name', '%'.$name.'%');
        }
        return $this;
    }

    public function filterByCity(String $city){
        if ($city != null){
            $this->query = $this->query->where('city' , $city);
        }
        return $this;
    }

    public function filterByProvince(String $province){
        if ($province != null){
            $this->query = $this->query->where('province' , $province);
        }
        return $this;
    }

    public function filterByDistrict(String $district){
        if ($district != null){
            $this->query = $this->query->where('district' , $district);
        }
        return $this;
    }

    public function Ban(){
        $this->query = $this->query->where('is_ban' , 0);
        return $this;
    }

    public function withUser(){
        $this->query = $this->query->with(['user']);
        return $this;
    }
    public function withRoom(){
        //TODO
        $this->query = $this->query->with(['room']);
        return $this;
    }

    public function withRoomAndRoomTypeAndRoomPrice(){
        $this->query = $this->query->with(['room' => function($query){
            return $query->with(['RoomType' , 'RoomPrice']);
        }]);
        return $this;
    }
    public function withRoomByStatus(){
        $this->query = $this->query->with(['room' => function($query){
            return $query->where('status' , 0)->with(['RoomType']);
        }]);
        return $this;
    }
    public function withRoomAndUser(){
        $this->query = $this->query->with(['user' , 'room' => function($query){
            return $query->with(['RoomType' , 'RoomPrice' , 'RoomPassword']);
        }]);
        return $this;
    }
}